import tkinter as tk
from tkinter import colorchooser, ttk

class DrawingTool:
    def __init__(self, root):
        # 设置主窗口
        self.root = root
        self.root.title("简易绘画工具")
        self.root.geometry("800x600")
        self.root.resizable(True, True)
        
        # 初始化绘画参数
        self.current_color = "black"
        self.brush_size = 5
        self.is_drawing = False
        
        # 创建UI组件
        self.create_widgets()
        
        # 绑定鼠标事件
        self.canvas.bind("<Button-1>", self.start_drawing)
        self.canvas.bind("<B1-Motion>", self.draw)
        self.canvas.bind("<ButtonRelease-1>", self.stop_drawing)
    
    def create_widgets(self):
        # 创建顶部控制面板
        control_frame = ttk.Frame(self.root, padding="10")
        control_frame.pack(fill=tk.X)
        
        # 颜色选择按钮
        ttk.Button(
            control_frame, 
            text="选择颜色", 
            command=self.choose_color
        ).pack(side=tk.LEFT, padx=5)
        
        # 画笔大小选择
        ttk.Label(control_frame, text="画笔大小:").pack(side=tk.LEFT, padx=5)
        self.size_scale = ttk.Scale(
            control_frame, 
            from_=1, 
            to=50, 
            orient=tk.HORIZONTAL,
            length=150,
            command=self.set_brush_size
        )
        self.size_scale.set(self.brush_size)
        self.size_scale.pack(side=tk.LEFT, padx=5)
        
        # 清除画布按钮
        ttk.Button(
            control_frame, 
            text="清除画布", 
            command=self.clear_canvas
        ).pack(side=tk.RIGHT, padx=5)
        
        # 创建画布
        self.canvas = tk.Canvas(self.root, bg="white")
        self.canvas.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
        
        # 创建状态栏显示当前画笔信息
        self.status_bar = ttk.Label(
            self.root, 
            text=f"颜色: {self.current_color} | 画笔大小: {self.brush_size}", 
            relief=tk.SUNKEN, 
            anchor=tk.W
        )
        self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)
    
    def choose_color(self):
        """选择绘画颜色"""
        color = colorchooser.askcolor(title="选择颜色")[1]
        if color:
            self.current_color = color
            self.update_status()
    
    def set_brush_size(self, value):
        """设置画笔大小"""
        self.brush_size = int(float(value))
        self.update_status()
    
    def update_status(self):
        """更新状态栏信息"""
        self.status_bar.config(
            text=f"颜色: {self.current_color} | 画笔大小: {self.brush_size}"
        )
    
    def start_drawing(self, event):
        """开始绘画"""
        self.is_drawing = True
        # 在点击位置画一个点
        self.draw(event)
    
    def draw(self, event):
        """绘画动作"""
        if self.is_drawing:
            # 获取当前鼠标位置
            x, y = event.x, event.y
            
            # 画一个圆形作为画笔痕迹
            self.canvas.create_oval(
                x - self.brush_size, y - self.brush_size,
                x + self.brush_size, y + self.brush_size,
                fill=self.current_color,
                outline=self.current_color
            )
    
    def stop_drawing(self, event):
        """停止绘画"""
        self.is_drawing = False
    
    def clear_canvas(self):
        """清除画布"""
        self.canvas.delete("all")

if __name__ == "__main__":
    root = tk.Tk()
    app = DrawingTool(root)
    root.mainloop()
